package com.windoent.yugioh.ui.activity.saishi;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mobstat.StatService;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.brvah.OnGoingDuiJueSearchListAdapter;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.bean.OnGoingDuiZhenBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.RoundImageView;
import com.windoent.yugioh.widget.SearchView;
import com.windoent.yugioh.widget.dialog.AuthSubmitGradeDialog;
import com.windoent.yugioh.widget.dialog.PanFaUserDetailsDialog;
import com.windoent.yugioh.widget.dialog.SubmitGradeAgainstDialog;
import com.windoent.yugioh.widget.dialog.SucessFailAutoDismissDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.List;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/20 14:03
 * desc   :搜索对阵
 */
public class SearchAgainstOnGoingActivity extends BaseActivity {
    private View mView;
    private LinearLayout mLlBack;
    private SearchView mSearch;
    private SmartRefreshLayout mSmartLayout;
    private RecyclerView mRecyclerView;
    private String limit = Constants.PAGETEN;
    private int mCount = 1;
    private String id;
    private String text = "";
    private OnGoingDuiJueSearchListAdapter adapter;
    private String roles;
    private String round;
    private String statusId = "";
    private boolean ended;
    private String statusid = "";

    @Override
    public int getLayout() {
        return R.layout.activity_saishi_ongo_ing_search;
    }

    @Override
    public void initView() {
        super.initView();

        mView = (View) findViewById(R.id.view);
        immersiveStatusBar(mView);
        mLlBack = (LinearLayout) findViewById(R.id.ll_back);
        mSearch = (SearchView) findViewById(R.id.search);
        mSmartLayout = (SmartRefreshLayout) findViewById(R.id.smart_layout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        round = intent.getStringExtra("round");
        statusId = intent.getStringExtra("statusId");
        ended = intent.getBooleanExtra("ended", false);
        initAdapter();
    }

    @Override
    public void initListener() {
        super.initListener();
        mLlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mSearch.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH || i == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    //此处做逻辑处理
                    text = mSearch.getEditStr();
                    initRefreshData(text);
                    fresh(text);
                    return true;
                }
                return false;
            }
        });
    }

    private void initRefreshData(String text) {
        //下拉刷新
        mSmartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                fresh(text);
            }
        });
        //加载更多
        mSmartLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMore(text);
            }
        });
    }

    private void loadMore(String text) {
        mCount = mCount + 1;
        getOnGoingAgaistListData(text, mCount + "", limit, false);
    }

    private void fresh(String text) {
        mCount = 1;
        getOnGoingAgaistListData(text, mCount + "", limit, true);
    }

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new OnGoingDuiJueSearchListAdapter();
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.recycler_view_empty_duizhen, mRecyclerView, false));
        adapter.setAnimationEnable(false);
        mRecyclerView.setAdapter(adapter);
        if (ended) {
            return;
        }
        adapter.addChildClickViewIds(R.id.rl_background, R.id.btn_get_code, R.id.rl_left, R.id.rl_right);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter, @NonNull View view, int position) {
                List<OnGoingDuiZhenBean.DataBean.ResBean> data = adapter.getData();
                if (data != null) {
                    OnGoingDuiZhenBean.DataBean.ResBean resBean = data.get(position);
                    if (resBean != null) {
                        int status_id = resBean.getStatus_id();
                        boolean is_verify = resBean.isIs_verify();
                        boolean customerReferee = BaseMethod.notNull(roles) && roles.equals("customerReferee");
                        boolean pReferee = BaseMethod.notNull(roles) && roles.equals("pReferee");
                        boolean referee = BaseMethod.notNull(roles) && roles.equals("referee");
                        switch (view.getId()) {
                            case R.id.rl_left:
                                if (customerReferee || pReferee || referee) {
                                    getPanFaStyleList(resBean.getA_nickname(), id, resBean.getA_id() + "", resBean.getId() + "");
                                }
                                break;
                            case R.id.rl_right:
                                if (customerReferee || pReferee || referee) {
                                    getPanFaStyleList(resBean.getB_nickname(), id, resBean.getB_id() + "", resBean.getId() + "");
                                }
                                break;
                            case R.id.btn_get_code:
                                if (status_id == 3 && is_verify && (customerReferee || pReferee)) {
                                    //主裁判修改成ji
                                    showModifyGradeDialog(resBean, resBean.getId() + "", BaseMethod.notNull(statusId) && statusId.equals("5"));
                                }
                                break;
                        }
                    }

                }
            }
        });
    }

    //判罚类型列表
    private void getPanFaStyleList(String name, String gameId, String userId, String vid) {
        PhoneLive.getPanFaStyleList(getUserToken(), gameId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            showUserPanFaDialog(name, gameId, userId, response, vid);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //用户对阵判罚记录
    private void showUserPanFaDialog(String name, String gameId, String userId, String response, String vId) {
        PanFaUserDetailsDialog dialog = new PanFaUserDetailsDialog(mContext);
        dialog.setUserStr(name, gameId, userId, response);
        dialog.setOnclickListener(new PanFaUserDetailsDialog.onClickListener() {
            @Override
            public void onClickSure(String type_id, String remark) {
                panfaPlayers(vId, userId, type_id, remark);
            }

            @Override
            public void clickMore() {
                Intent intent = new Intent(mContext, PanFaDetailsActivity.class);
                intent.putExtra("gameId", gameId);
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });
        dialog.show();
    }

    //判罚操作
    private void panfaPlayers(String vid, String userId, String type_id, String remark) {
        PhoneLive.panfaPlayers(getUserToken(), vid, userId, type_id, remark, statusId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                if (BaseUtils.isJSONValid(response)) {
                    GeneralBean fromJson = gson.fromJson(response, GeneralBean.class);
                    if (fromJson != null) {
                        int code = fromJson.getCode();
                        String msg = fromJson.getMsg();
                        if (code == 200) {
                            BaseMethod.showSucessDialog(mContext, BaseMethod.notNullDefaultValue(msg, getString(R.string.string_panfa_player_sucess)), new SucessFailAutoDismissDialog.OnInterface() {
                                @Override
                                public void onDissmiss() {

                                }
                            });
                        } else {
                            BaseMethod.showFailDialog(mContext, getString(R.string.string_panfa_player_fail), BaseMethod.notNullDefaultValueNull(msg));
                        }
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //比赛对阵信息
    private void getOnGoingAgaistListData(String title, String page, String limit, boolean fresh) {
        showLoadingDialog("");
        PhoneLive.getDuiZHenGameDetails(getUserToken(), id, "all", round, statusId, title, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
                freshFail(fresh);
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            OnGoingDuiZhenBean json = gson.fromJson(response, OnGoingDuiZhenBean.class);
                            if (json != null) {
                                OnGoingDuiZhenBean.DataBean data = json.getData();
                                if (data != null) {
                                    OnGoingDuiZhenBean.DataBean.SelectFilterBean selectFilter = data.getSelectFilter();
                                    if (selectFilter != null) {
                                        statusid = selectFilter.getStatusId() + "";
                                    }
                                    List<OnGoingDuiZhenBean.DataBean.ResBean> res = data.getRes();
                                    roles = data.getRoles();
                                    boolean player = BaseMethod.notNull(roles) && roles.equals("player");
                                    if (res != null && res.size() > 0) {
                                        if (adapter != null) {
                                            OnGoingDuiZhenBean.DataBean.ResBean resZero = res.get(0);
                                            boolean is_join = resZero.isIs_join();
                                            if (adapter != null && adapter.getHeaderLayoutCount() > 0) {
                                                adapter.removeAllHeaderView();
                                            }
                                            if (is_join) {
                                                addHeader(adapter, resZero);
                                                res.remove(0);
                                            } else {
                                                if (adapter.hasEmptyView()) {
                                                    adapter.setUseEmpty(true);
                                                }
                                            }
                                        }
                                    }
                                    assert adapter != null;
                                    adapter.setRole(roles);
                                    //瑞士轮显示修改
                                    boolean ruishilun = data.isEditButton();
                                    if (ended) {
                                        adapter.setIsRuiShiLun(false);
                                    } else
                                        adapter.setIsRuiShiLun(ruishilun);
                                    if (fresh) {
                                        mSmartLayout.finishRefresh(true);
                                        adapter.setNewInstance(res);
                                    } else {
                                        mSmartLayout.finishLoadMore(true);
                                        adapter.addData(res);
                                    }
                                }
                            }
                        } else freshFail(fresh);
                    } catch (JSONException e) {
                        freshFail(fresh);
                        e.printStackTrace();
                    }
                } else {
                    freshFail(fresh);
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //修该成绩
    private void showModifyGradeDialog(OnGoingDuiZhenBean.DataBean.ResBean resBean, String vid, boolean outGame) {
        AuthSubmitGradeDialog dialog = new AuthSubmitGradeDialog(mContext);
        dialog.setInfo(resBean, id, outGame, statusid);
        dialog.setOnclickListener(new AuthSubmitGradeDialog.onClickListener() {
            @Override
            public void onClick(int selectPosition) {//1左胜利，2右胜，3平,4双败
                mainRefereeEditorGrades(vid, selectPosition + "");
            }
        });
        dialog.show();
    }

    //主裁判修改成绩
    private void mainRefereeEditorGrades(String vid, String grade) {
        showLoadingDialog("");
        PhoneLive.mainRefereeEditorGrades(getUserToken(), vid, grade,statusId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    GeneralBean fromJson = gson.fromJson(response, GeneralBean.class);
                    if (fromJson != null) {
                        int code = fromJson.getCode();
                        String msg = fromJson.getMsg();
                        if (code == 200) {
                            BaseMethod.showSucessDialog(mContext, BaseMethod.notNullDefaultValue(msg, getString(R.string.string_refree_modify_grade_sucess)), new SucessFailAutoDismissDialog.OnInterface() {
                                @Override
                                public void onDissmiss() {
                                    //TODO 刷新
                                    initRefreshData(text);
                                    fresh(text);
                                }
                            });
                        } else {
                            BaseMethod.showFailDialog(mContext, getString(R.string.string_refree_modify_grade_fail), BaseMethod.notNullDefaultValueNull(msg));
                        }
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //自己的参加的比赛
    private void addHeader(OnGoingDuiJueSearchListAdapter adapter, OnGoingDuiZhenBean.DataBean.ResBean item) {
        View headView = getLayoutInflater().inflate(R.layout.head_rv_view_ongoing_xuanshou_games, (ViewGroup) mRecyclerView.getParent(), false);

        TextView submirBtn = headView.findViewById(R.id.btn_get_code);
        ImageView iv_vs = headView.findViewById(R.id.iv_vs);
        LinearLayout ll_left_normal = headView.findViewById(R.id.ll_left_normal);
        LinearLayout ll_left_big = headView.findViewById(R.id.ll_left_big);
        LinearLayout ll_luck_lun_null_right = headView.findViewById(R.id.ll_luck_lun_null_right);
        LinearLayout ll_right_normal = headView.findViewById(R.id.ll_right_normal);
        LinearLayout ll_right_big = headView.findViewById(R.id.ll_right_big);

        TextView tv_game_id_xuanshou = headView.findViewById(R.id.tv_game_id_xuanshou);
        TextView tv_game_number_xuanshou = headView.findViewById(R.id.tv_game_number_xuanshou);
        TextView tv_game_status = headView.findViewById(R.id.tv_game_status);
        //左边
        //正常大小
        RoundImageView iv_avatar_left_normal = headView.findViewById(R.id.iv_avatar_left_xuanshou);
        TextView iv_id_left_normal = headView.findViewById(R.id.iv_id_left_xuanshou);
        TextView iv_name_left_xuanshou = headView.findViewById(R.id.iv_name_left_xuanshou);
        //大
        RoundImageView iv_avatar_left_big = headView.findViewById(R.id.iv_avatar_left_xuanshou_big);
        ImageView iv_avatar_left_huangguan_big = headView.findViewById(R.id.iv_sucess_bg_left_huangguan);
        ImageView iv_avatar_left_scry_big = headView.findViewById(R.id.iv_sucess_bg_left_scry_emo);
        TextView iv_id_left_big = headView.findViewById(R.id.iv_id_left_xuanshou_big);
        TextView iv_name_left_big = headView.findViewById(R.id.iv_name_left_xuanshou_big);

        //右边
        //正常
        RoundImageView iv_avatar_right_normal = headView.findViewById(R.id.iv_avatar_right_xuanshou);
        TextView iv_id_right_normal = headView.findViewById(R.id.iv_id_right_xuanshou);
        TextView iv_name_right_xuanshou = headView.findViewById(R.id.iv_name_right_xuanshou);
        //大
        RoundImageView iv_avatar_right_big = headView.findViewById(R.id.iv_avatar_right_xuanshou_big);
        ImageView iv_avatar_right_huangguan_big = headView.findViewById(R.id.iv_sucess_bg_right_huangguan);
        ImageView iv_avatar_right_scry_big = headView.findViewById(R.id.iv_sucess_bg_right_scry_emo);
        TextView iv_id_right_big = headView.findViewById(R.id.iv_id_right_xuanshou_big);
        TextView iv_name_right_big = headView.findViewById(R.id.iv_name_right_xuanshou_big);

        int status_id = item.getStatus_id();//"1": "进行中", "2": "审核中","3": "成绩有效","4": "成绩无效"
        int a_result = item.getA_result();
        int a_id = item.getA_id();// "0": "还未提交成绩","1": "胜", "2": "平","3": "负","4": "双负"
        int b_result = item.getB_result();
        int b_id = item.getB_id();

        //数据
        tv_game_id_xuanshou.setText(getString(R.string.string_game_id_colon) + BaseMethod.notNullDefaultValueNull(item.getId() + ""));
        tv_game_number_xuanshou.setText(BaseMethod.notNullDefaultValueNull(item.getDesk_name()));
        SimpleUtils.loadImageForView(mContext, iv_avatar_left_normal, item.getA_avatar(), R.drawable.img_official_avatar_android);
        SimpleUtils.loadImageForView(mContext, iv_avatar_left_big, item.getA_avatar(), R.drawable.img_official_avatar_android);
        SimpleUtils.loadImageForView(mContext, iv_avatar_right_normal, item.getB_avatar(), R.drawable.img_official_avatar_android);
        SimpleUtils.loadImageForView(mContext, iv_avatar_right_big, item.getB_avatar(), R.drawable.img_official_avatar_android);
        iv_id_left_normal.setText(getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getA_id() + ""));
        iv_id_left_big.setText(getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getA_id() + ""));
        iv_id_right_normal.setText(getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getB_id() + ""));
        iv_id_right_big.setText(getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getB_id() + ""));
        iv_name_left_xuanshou.setText(BaseMethod.notNullDefaultValueNull(item.getA_nickname()));
        iv_name_left_big.setText(BaseMethod.notNullDefaultValueNull(item.getA_nickname()));
        iv_name_right_xuanshou.setText(BaseMethod.notNullDefaultValueNull(item.getB_nickname()));
        iv_name_right_big.setText(BaseMethod.notNullDefaultValueNull(item.getB_nickname()));

        submirBtn.setVisibility(status_id == 1 || status_id == 2 ? View.VISIBLE : View.GONE);
        if (status_id == 1) {
            submirBtn.setSelected(false);
            submirBtn.setText(getString(R.string.string_submit_grade));
            iv_vs.setBackgroundResource(R.drawable.drawable_vs_beauty);
            ll_left_normal.setVisibility(View.VISIBLE);
            ll_right_normal.setVisibility(View.VISIBLE);
            ll_left_big.setVisibility(View.GONE);
            ll_right_big.setVisibility(View.GONE);
            ll_luck_lun_null_right.setVisibility(View.GONE);

            //数据
            tv_game_status.setText(getString(R.string.string_ongoing));
            setHeaderGray(true, true, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);
        } else if (status_id == 2) {
            submirBtn.setSelected(true);
            submirBtn.setText(getString(R.string.string_reviewing));
            iv_vs.setBackgroundResource(R.drawable.drawable_vs_beauty);
            ll_left_normal.setVisibility(View.VISIBLE);
            ll_right_normal.setVisibility(View.VISIBLE);
            ll_left_big.setVisibility(View.GONE);
            ll_right_big.setVisibility(View.GONE);
            ll_luck_lun_null_right.setVisibility(View.GONE);

            //数据
            tv_game_status.setText(getString(R.string.string_reviewing));
            setHeaderGray(true, true, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);
        } else {
            //数据
            tv_game_status.setText(getString(R.string.string_has_ended));
            if (a_id == getAccountId() && b_id != 0) {
                //左边是自己
                if (status_id == 3 && a_result == 1) {//胜利
                    iv_vs.setBackgroundResource(R.drawable.drawable_victory);
                    setHeaderGray(true, true, false, false, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.GONE);
                    ll_left_big.setVisibility(View.VISIBLE);
                    ll_right_normal.setVisibility(View.VISIBLE);
                    ll_right_big.setVisibility(View.GONE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                    iv_avatar_left_huangguan_big.setVisibility(View.VISIBLE);
                    iv_avatar_left_scry_big.setVisibility(View.GONE);
                } else if (status_id == 3 && (a_result == 2)) {//平
                    iv_vs.setBackgroundResource(R.drawable.drawable_ping);
                    setHeaderGray(true, true, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.VISIBLE);
                    ll_right_normal.setVisibility(View.VISIBLE);
                    ll_left_big.setVisibility(View.GONE);
                    ll_right_big.setVisibility(View.GONE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                } else if (status_id == 3 && (a_result == 4)) {//双负
                    iv_vs.setBackgroundResource(R.drawable.drawable_double_defeat);
                    setHeaderGray(false, false, false, false, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.VISIBLE);
                    ll_right_normal.setVisibility(View.VISIBLE);
                    ll_left_big.setVisibility(View.GONE);
                    ll_right_big.setVisibility(View.GONE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                } else if (status_id == 3 && a_result == 3) {//负
                    iv_vs.setBackgroundResource(R.drawable.drawable_defeat);
                    setHeaderGray(false, false, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.GONE);
                    ll_left_big.setVisibility(View.VISIBLE);
                    ll_right_normal.setVisibility(View.VISIBLE);
                    ll_right_big.setVisibility(View.GONE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                    iv_avatar_left_scry_big.setVisibility(View.VISIBLE);
                    iv_avatar_left_huangguan_big.setVisibility(View.GONE);
                }
            } else if (b_id != 0 && b_id == getAccountId()) {
                if (status_id == 3 && b_result == 1) {//胜利
                    iv_vs.setBackgroundResource(R.drawable.drawable_victory);
                    setHeaderGray(false, false, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.VISIBLE);
                    ll_left_big.setVisibility(View.GONE);
                    ll_right_normal.setVisibility(View.GONE);
                    ll_right_big.setVisibility(View.VISIBLE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                    iv_avatar_right_huangguan_big.setVisibility(View.VISIBLE);
                    iv_avatar_right_scry_big.setVisibility(View.GONE);
                } else if (status_id == 3 && (a_result == 2)) {//平
                    iv_vs.setBackgroundResource(R.drawable.drawable_ping);
                    setHeaderGray(true, true, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.VISIBLE);
                    ll_right_normal.setVisibility(View.VISIBLE);
                    ll_left_big.setVisibility(View.GONE);
                    ll_right_big.setVisibility(View.GONE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                } else if (status_id == 3 && (a_result == 4)) {//双负
                    iv_vs.setBackgroundResource(R.drawable.drawable_double_defeat);
                    setHeaderGray(false, false, false, false, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.VISIBLE);
                    ll_right_normal.setVisibility(View.VISIBLE);
                    ll_left_big.setVisibility(View.GONE);
                    ll_right_big.setVisibility(View.GONE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                } else if (status_id == 3 && a_result == 3) {//负
                    iv_vs.setBackgroundResource(R.drawable.drawable_defeat);
                    setHeaderGray(true, true, false, false, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);

                    ll_left_normal.setVisibility(View.VISIBLE);
                    ll_left_big.setVisibility(View.GONE);
                    ll_right_normal.setVisibility(View.GONE);
                    ll_right_big.setVisibility(View.VISIBLE);
                    ll_luck_lun_null_right.setVisibility(View.GONE);
                    iv_avatar_right_scry_big.setVisibility(View.VISIBLE);
                    iv_avatar_right_huangguan_big.setVisibility(View.GONE);
                }
            } else if (b_id == 0 && item.isIs_verify()) {
                //轮空
                iv_vs.setBackgroundResource(R.drawable.drawable_victory);
                setHeaderGray(true, true, true, true, iv_avatar_left_normal, iv_id_left_normal, iv_name_left_xuanshou, iv_avatar_left_big, iv_id_left_big, iv_name_left_big, iv_avatar_right_normal, iv_id_right_normal, iv_name_right_xuanshou, iv_avatar_right_big, iv_id_right_big, iv_name_right_big);
                ll_left_normal.setVisibility(View.GONE);
                ll_right_normal.setVisibility(View.GONE);
                ll_left_big.setVisibility(View.VISIBLE);
                ll_right_big.setVisibility(View.GONE);
                ll_luck_lun_null_right.setVisibility(View.VISIBLE);
                iv_avatar_left_huangguan_big.setVisibility(View.VISIBLE);
                iv_avatar_left_scry_big.setVisibility(View.GONE);
            }
        }
        submirBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (status_id == 1) {
                    if (b_id != 0 && (a_id == getAccountId() || b_id == getAccountId()))
                        showSubmitGradeDialog(item, BaseMethod.notNull(statusId) && statusId.equals("5"));
                    else
                        BaseMethod.showFailDialog(mContext, getString(R.string.string_canot_submit_grade_warning), getString(R.string.string_canot_submit_grade_reason_warning));
                } else if (status_id == 2) {
                    BaseMethod.showSucessDialog(mContext, getString(R.string.string_reviewing_warning), null);
                }
            }
        });
        if (adapter != null) {
            adapter.addHeaderView(headView);
            adapter.setHeaderWithEmptyEnable(true);
            if (adapter.hasEmptyView()) {
                adapter.setUseEmpty(false);
            }
        }

    }

    //提交成绩
    private void showSubmitGradeDialog(OnGoingDuiZhenBean.DataBean.ResBean item, boolean outGame) {
        SubmitGradeAgainstDialog dialog = new SubmitGradeAgainstDialog(mContext);
        dialog.setDataInfo(item, outGame);
        dialog.setOnclickListener(new SubmitGradeAgainstDialog.onClickListener() {
            @Override
            public void onClick(int selectPosition) {//1胜利，2平，3失败  1：胜 2：平 3：负
                playsSubmitGrade(selectPosition + "");
            }
        });
        dialog.show();
    }

    //选手自己提交成绩
    private void playsSubmitGrade(String grade) {
        showLoadingDialog("");
        PhoneLive.playsSubmitGrade(getUserToken(), id, grade, statusId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    GeneralBean fromJson = gson.fromJson(response, GeneralBean.class);
                    if (fromJson != null) {
                        int code = fromJson.getCode();
                        String msg = fromJson.getMsg();
                        if (code == 200) {
                            BaseMethod.showSucessDialog(mContext, BaseMethod.notNullDefaultValue(msg, getString(R.string.string_reward_sure_submit)), new SucessFailAutoDismissDialog.OnInterface() {
                                @Override
                                public void onDissmiss() {
                                    initRefreshData(text);
                                    fresh(text);
                                }
                            });
                        } else {
                            BaseMethod.showFailDialog(mContext, getString(R.string.string_submit_grade_fail), BaseMethod.notNullDefaultValueNull(msg));
                        }
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //头布局亮暗
    private void setHeaderGray(boolean leftNormal, boolean leftBig, boolean rightNormal, boolean rightBig, RoundImageView iv_avatar_left_normal, TextView iv_id_left_normal, TextView iv_name_left_xuanshou, RoundImageView iv_avatar_left_big, TextView iv_id_left_big, TextView iv_name_left_big, RoundImageView iv_avatar_right_normal, TextView iv_id_right_normal, TextView iv_name_right_xuanshou, RoundImageView iv_avatar_right_big, TextView iv_id_right_big, TextView iv_name_right_big) {
        iv_avatar_left_normal.setShape(!leftNormal);
        iv_avatar_left_big.setShape(!leftBig);
        iv_avatar_right_normal.setShape(!rightNormal);
        iv_avatar_right_big.setShape(!rightBig);
        iv_id_left_normal.setSelected(leftNormal);
        iv_id_left_big.setSelected(leftBig);
        iv_id_right_normal.setSelected(rightNormal);
        iv_id_right_big.setSelected(rightBig);
        iv_name_left_xuanshou.setSelected(leftNormal);
        iv_name_left_big.setSelected(leftBig);
        iv_name_right_xuanshou.setSelected(rightNormal);
        iv_name_right_big.setSelected(rightBig);
    }

    //刷新失败
    private void freshFail(boolean fresh) {
        if (fresh) {
            adapter.setNewInstance(null);
            mSmartLayout.finishRefresh(false);
        } else {
            mSmartLayout.finishLoadMore(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "赛事搜索对阵界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "赛事搜索对阵界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
